#!/bin/sh
logpath=/root/log/main.log

# 检测日志文件目录的大小
ret=$(du -s /root | sed -e "s/[[:space:]]//g")
ret=${ret%%/*}
count=$((ret + 1))
log_maxsize=$((10*1024*1024*1024))   #最大日志存储10G
echo ${log_maxsize}
if [ $count -gt $log_maxsize ]; 
then
        #清除日志文件
        rm -rf /root/log/log_*.log
        echo "日志文件太大，删除历史日志文件..."
else
        echo "日志文件大小: ${count}"
fi

if [ -f $logpath ];
then
        # 将日志文件 保留30天
        echo "备份日志文件...."
        mv $logpath /root/log/log_$(date "+%m%d_%H%M").log
else
        echo "创建日志文件...."
fi

echo "--------------------------------------------------------" > ${logpath}
# 挂载磁盘
echo "检测ceph服务运行状态" >> ${logpath}
while true;do
    systemctl status ceph-mon@* &>/dev/null    
    if [ $? -eq 0 ]
    then
            echo "ceph 服务正在运行" >> ${logpath}
            break;
    else
            echo "[ERROR] ceph 服务没有运行" >> ${logpath}
            # exit 1;
    fi
    sleep 3s
done

echo "检查磁盘是否挂载" >> ${logpath}
rcount=0
maxrecount=60
while true; do
	if [ $rcount -gt $maxrcount ];
	then
		reboot
		break;
	else
		$((rcount++))
	fi

        #mount -a
	mount -t ceph 192.168.70.11:/ /mnt/cephfs/
        df | grep /mnt/cephfs &> /dev/null
        if [ $? -eq 0 ]
        then
                echo "ceph 磁盘已经挂载，挂载地址：/mnt/ceph, 输入 df -h 查看磁盘大小" >> ${logpath}
                break;
        else        
                echo "[ERROR] ceph 磁盘未挂载,挂载次数：${rcount} 挂载位置： /mnt/cephfs, 重新挂载中... " >> ${logpath}       
        fi
        sleep 3s;
done

echo "--------------------------------------------------------" >> ${logpath}
echo "检测驱动程序是否启动" >> ${logpath}
lsmod | grep xbmd >> ${logpath}
mknod /dev/xbmd c 255 1
echo "--------------------------------------------------------" >> ${logpath}
while true;do
	echo "启动主程序" >> ${logpath}
	cd /home/han/linux_cpu/
	# ./start.sh
	cd ./src
	./a.out >> ${logpath}
	echo "-------------------------主程序异常结束运行，重新启动。。。-------------------------------" >> ${logpath}
done
